package net.chathu.airgw.packets.gw.clientpackets 
{
	import air.update.utils.StringUtils;
	import net.chathu.airgw.Config;
	import net.chathu.airgw.model.Room;
	import net.chathu.airgw.packets.gw.serverpackets.ErrorNoSuchChannelPacket;
	import net.chathu.airgw.packets.gw.serverpackets.PartPacket;
	import net.chathu.airgw.util.ByteArrayUtil;
	import net.chathu.airgw.util.StringUtil;
	/**
	 * ...
	 * @author ze
	 */
	public class CMDPart extends IRCReceivablePacket 
	{
		
		public function CMDPart() 
		{
			
		}
		
		override public function execute():void {
			if (data.length > 1)
			{
				var roomName:String = data[1];
				var room:Room = sessionData.chatData.rooms.getRoomByName(roomName);
				if (room)
				{
					sendToChat(ByteArrayUtil.convertParams(["unsub", "rooms/" + room.id + "/users"]));
				} else {
					roomName = roomName.toLowerCase();
					if (sessionData.isInGroup(roomName)) {
						//p2p szobabol lepne ki..
						sessionData.soul.p2pChatConnection.leaveGroup(roomName);
						//TODO: kell ide part packet?
						sendToClient(new PartPacket(sessionData.yourHost, roomName));
					} else if ( roomName == Config.USERSLIST_CHANNEL) {
						if (sessionData.chatData.isInUsersRoom)
						{
							sessionData.chatData.isInUsersRoom = false;
							sendToClient(new PartPacket(sessionData.yourHost, Config.USERSLIST_CHANNEL));
						}
					} else {
						sendToClient(new ErrorNoSuchChannelPacket(roomName));
					}
				}
			}
		}
		
		public static function getShortUsageDescription():String {
			return "Szoba elhagyás";
		}
		
		public static function getUsageDescription():String {
			return "Példa:\n" +
				"/part #eloszoba\n\n" +
				"Eredmény:\n" + 
				"Sikeresen elhagytad az Előszoba szobát.";
		}
	}
}